﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Windows.Forms;
using QuanLyBanHang.DataConnection;

namespace QuanLyBanHang.QuerySQL
{
    class ClassQLHDB
    {
        Connect conn = new Connect();
        //show hoa don
        public DataTable ShowHD()
        {
            DataTable tb = new DataTable(); ;
            try
            {
                string sql = "select MAHD,HOTEN,TENNV,NGAYLAP,TONGTIEN from HOADON,KHACHHANG,NHANVIEN where HOADON.MAKH = KHACHHANG.MAKH and HOADON.MANV = NHANVIEN.MANV";
                tb = conn.getTable(sql);
            }
            catch (Exception e)
            {
                MessageBox.Show("Lỗi: " + e.Message);
            }
            return tb;
        }
        //show chi tiet hoa don
        public DataTable ShowCTHD(int i,string mahd = null)
        {
            DataTable tb = new DataTable();
            switch(i){
                case 0:
                    {
                        try
                        {
                            string sql = "select *from CHITIET_HD";
                            tb = conn.getTable(sql);
                        }
                        catch (Exception e)
                        {
                            MessageBox.Show("Lỗi: " + e.Message);
                        }
                    }
                    break;
                case 1:
                    {

                        try
                        {
                            string sql = "select *from CHITIET_HD where MAHD like '%"+mahd+"%'";
                            tb = conn.getTable(sql);
                        }
                        catch (Exception e)
                        {
                            MessageBox.Show("Lỗi: " + e.Message);
                        }
                        break;
                    }
            }
            return tb;
        }

        //
        public DataTable TKCT(string mahd)
        {
            DataTable tb = new DataTable(); ;
            try
            {
                string sql = "select *from CHITIET_HD where MAHD = '"+mahd+"' ";
                tb = conn.getTable(sql);
            }
            catch (Exception e)
            {
                MessageBox.Show("Lỗi: " + e.Message);
            }
            return tb;
        }
        //
        public DataTable TKHD(int i, string mahd, string tenkh)
        {
            DataTable dt = new DataTable();
            switch (i)
            {
                case 0:
                    {
                        try
                        {
                            String sql = "select MAHD,HOTEN,TENNV,NGAYLAP,TONGTIEN from HOADON,KHACHHANG,NHANVIEN where MAHD like '%" + mahd + "%' and HOADON.MAKH = KHACHHANG.MAKH and HOADON.MANV = NHANVIEN.MANV";
                            dt = conn.getTable(sql);
                        }
                        catch (Exception e)
                        {
                            MessageBox.Show("Lỗi: " + e.Message);
                        }
                        break;
                    }
                case 1:
                    {
                        try
                        {
                            String sql = "select MAHD,HOTEN,TENNV,NGAYLAP,TONGTIEN from HOADON,KHACHHANG,NHANVIEN where HOTEN like N'%" + tenkh + "%' and HOADON.MAKH = KHACHHANG.MAKH and HOADON.MANV = NHANVIEN.MANV";
                            dt = conn.getTable(sql);
                        }
                        catch (Exception e)
                        {
                            MessageBox.Show("Lỗi: " + e.Message);
                        }
                        break;
                    }
            }
            return dt;
        }
        //
        public void XoaHD(string mahd)
        {
            try
            {
                string sql = "delete HOADON where MAHD = '" + mahd + "'";
                conn.ExcuteQuerySql(sql);
                MessageBox.Show("Xóa thành công !!!");
            }
            catch (Exception e)
            {
                MessageBox.Show("Lỗi: " + e.Message);
            }
        }
        public void XoaCTHD(string mahd)
        {
            try
            {
                string sql = "delete CHITIET_HD where MAHD = '" + mahd + "'";
                conn.ExcuteQuerySql(sql);
            }
            catch (Exception e)
            {
                MessageBox.Show("Lỗi: " + e.Message);
            }
        }
    }
}
